import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_jd/models/blog_model.dart';
import 'package:flutter_jd/pages/blog/blog/controller.dart';
import 'package:flutter_jd/routes/routing_constants.dart';
import 'package:flutter_jd/ui/widgets/refresh/pull_refresh.dart';
import 'package:flutter_jd/ui/widgets/body/top_bar.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get/route_manager.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

// 博客
class Blog extends GetView<BlogController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xFFF5F5F5),
      appBar: TopBar(
        title: '博客',
        isBack: true,
      ),
      body: GetBuilder<BlogController>(
        init: BlogController(),
        builder: (controller) {
          return PullRefresh(
            refreshController: controller.refreshController,
            onRefresh: controller.onRefresh,
            onLoading: controller.onLoading,
            child: ListView.builder(
              itemCount: controller.blogList.length,
              itemExtent: 130.h,
              itemBuilder: (BuildContext context, int index) {
                BlogModel? model = controller.blogList[index];
                return _buildList(model!, index);
              },
            ),
          );
        },
      ),
    );
  }

  _buildList(BlogModel model, index) {
    return Container(
      margin: EdgeInsets.only(top: 8.h),
      child: Ink(
        decoration: BoxDecoration(
          color: Colors.white,
        ),
        child: InkWell(
          onTap: () {
            Get.toNamed(BlogDetailRoute, arguments: {'id': model.id});
          },
          child: Container(
            padding: EdgeInsets.symmetric(
              vertical: 13.h,
              horizontal: 12.w,
            ),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Expanded(
                  child: Container(
                    padding: EdgeInsets.only(right: 12.w),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: [
                        Text(
                          model.title,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: 17.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(left: 1.w, top: 5.h),
                          child: Text(
                            model.brief,
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(
                              color: Color(0xFF333333),
                              fontSize: 14.sp,
                            ),
                          ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 10.h),
                          child: Row(
                            children: [
                              Container(
                                margin: EdgeInsets.only(top: 1.h),
                                child: Text(
                                  '${model.createTime} 发布',
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Color(0xFF999999),
                                    fontSize: 11.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                ),
                              ),
                              Container(
                                margin: EdgeInsets.only(
                                  left: 15.w,
                                  top: 1.h,
                                ),
                                child: Text(
                                  '${model.count} 阅读',
                                  overflow: TextOverflow.ellipsis,
                                  style: TextStyle(
                                    color: Color(0xFF999999),
                                    fontSize: 11.sp,
                                    fontWeight: FontWeight.w500,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                ClipRRect(
                  borderRadius: BorderRadius.circular(3.r),
                  child: Container(
                    width: 120.w,
                    height: 80.h,
                    color: Color(0xFFF2F2F2),
                    child: Image.network(
                      model.homeImg,
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
